package com.surpassun.emall.service.impl;

import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.surpassun.emall.model.Category;
import com.surpassun.emall.repository.CategoryRepository;
import com.surpassun.emall.service.CategoryService;
import com.surpassun.emall.service.StatisticsService;
import com.surpassun.emall.util.Constants;
import com.surpassun.emall.util.GetterUtil;
import com.surpassun.emall.util.PropsKeys;
import com.surpassun.emall.util.PropsUtil;
import com.surpassun.emall.util.QueryUtil;
import com.surpassun.emall.util.StringPool;
import com.surpassun.emall.util.StringUtil;
import com.surpassun.emall.util.Validator;

@Service
public class CategoryServiceImpl extends AbstractServiceImpl<Category> implements CategoryService {

	private Logger _log = LoggerFactory.getLogger(CategoryServiceImpl.class);
	
	@Inject
	CategoryRepository categoryRepository;
	
	@Inject
	StatisticsService statisticsService;
	
	public CategoryServiceImpl() {
		super(Category.class);
	}

	@Override
	public void addCategory(String name, Category parent) {
		if (Validator.isNotNull(name)) {
			Category category = new Category(name, parent);
			categoryRepository.add(category);
		}
	}

	@Override
	public void updateCategory(long categoryId, String name, Category parent) {
		Category category = super.get(categoryId);
		if (Validator.isNotNull(category)) {
			category.setName(name);
			category.setParent(parent);
			categoryRepository.update(category);
		}
	}

	@Override
	public List<Category> getPopularCategories() {
		int number = GetterUtil.getInteger(PropsUtil.getString(PropsKeys.SELECT_POPULAR_CATEGORIES_NUMBER));
		List<Long> referenceIds = statisticsService.getTopItems(Constants.STATISTICS_TYPE_CATEGORY, number);
		if (Validator.isNotEmpty(referenceIds)) {
			return categoryRepository.getPopularCategories(referenceIds);
		} else {
			return null;
		}
	}

	@Override
	public void initializeCategories(String[] productCategories, Category parentCategory) {
		for (String productCategory : productCategories) {
			if (Validator.isNull(parentCategory)) {
				String[] temp = StringUtil.split(productCategory, StringPool.POUND);
				if (Validator.isNotEmpty(temp) && temp.length == 2) {
					String parentCategoryName = temp[0];
					String[] childCategories = StringUtil.split(temp[1], StringPool.AT);
					
					//Add parent store type
					Category category = categoryRepository.getSingleByField("name", parentCategoryName, QueryUtil.EQUAL);
					if (Validator.isNull(category)) {
						category = new Category(parentCategoryName, null);
						categoryRepository.add(category);
						_log.info("New product category '" + parentCategoryName + "' added");
					}
					
					//Add child store types
					initializeCategories(childCategories, category);
				}
			} else {
				String categoryName = parentCategory.getName() + StringPool.PERIOD + productCategory;
				Category category = categoryRepository.getSingleByField("name", categoryName, QueryUtil.EQUAL);
				if (Validator.isNull(category)) {
					category = new Category(categoryName, parentCategory);
					categoryRepository.add(category);
					_log.info("New product sub-category '" + categoryName + "' added");
				}
			}
		}
	}

	@Override
	public List<Category> getRootCategories() {
		return categoryRepository.getRootCategories();
	}
}
